//
//  ZJMessageTableViewCell.m
//  ZJControlsClass
//
//  Created by 陈旭 on 2024/12/6.
//

#import "ZJMessageTableViewCell.h"
#import "ZJMessageInvigilateViewController.h"
#import "ZJMessageSignForViewController.h"
#import "ZJMessagePunishViewController.h"
#import "ZJMessageEarlyViewController.h"
#import "ZJMessageSysViewController.h"
#import "ZJMessageApprovelViewController.h"
#import "PendApprovalInfoViewController.h"
#import "HomeTransactPersonViewController.h"
#import "ZJMessageReadSatusNetworking.h"
#import "HomeOnlineExaminationViewController.h"
#import "SStringUtils.h"
#import "HomeTransferOrderInfoViewController.h"
#import "HomeTransforShowOrderInfoViewController.h"

@interface ZJMessageTableViewCell()

@property (nonatomic, strong) UIView *bgView;    // 背景

@property (nonatomic, strong) UIView *iconImageView;    // 左侧图标
@property (nonatomic, strong) UILabel *iconLabel;       // 图标中的文字
@property (nonatomic, strong) UILabel *titleLabel;       // 标题
@property (nonatomic, strong) UILabel *statusLabel;       // 状态
@property (nonatomic, strong) UIView *unReadView;    // 未读样式
@property (nonatomic, strong) UILabel *templateLabel;   // 模板昵称
@property (nonatomic, strong) UILabel *timeLabel;       // 时间
@property (nonatomic, strong) UILabel *contentLabel;    // 内容
@property (nonatomic, strong) UIView *separatorLine;    // 分割线
@property (nonatomic, strong) UIButton *sureBtn;    // 按钮

@end

@implementation ZJMessageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        //        self.contentView.backgroundColor = [UIColor whiteColor];
        //        self.frame =
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    //    self.contentView.backgroundColor = [UIColor whiteColor];
    //    self.contentView.layer.cornerRadius = 10;
    //    self.contentView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.bgView];
    
    [self.bgView addSubview:self.iconImageView];
    [self.iconImageView addSubview:self.iconLabel];
    [self.bgView addSubview:self.templateLabel];
    [self.bgView addSubview:self.timeLabel];
    
    [self.bgView addSubview:self.unReadView];
    [self.bgView addSubview:self.titleLabel];
    [self.bgView addSubview:self.statusLabel];
    
    
    [self.bgView addSubview:self.contentLabel];
    [self.bgView addSubview:self.separatorLine];
    
    [self.bgView addSubview:self.sureBtn];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        //        make.edges.equalTo(self.contentView);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.contentView.mas_bottom);
        make.top.equalTo(self.contentView.mas_top).offset(10);
    }];
    
    
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(12);
        make.top.equalTo(self.bgView).offset(12);
        make.size.mas_equalTo(CGSizeMake(20, 20));
        //        make.bottom.equalTo(self.bgView).offset(-12);
    }];
    
    [self.iconLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.iconImageView); // 居中显示
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-12);
        make.top.equalTo(self.iconImageView);
//        make.width.mas_lessThanOrEqualTo(120); // 最大宽度
    }];
    
    [self.templateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView.mas_right).offset(2);
        make.centerY.equalTo(self.iconImageView);
        make.right.equalTo(self.timeLabel.mas_left).offset(-10);
    }];
    
    
    [self.statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-12);
        make.centerY.equalTo(self.titleLabel);
        make.width.mas_lessThanOrEqualTo(80); // 最大宽度
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconImageView.mas_bottom).offset(20);
        //        make.top.
        //        make.left.equalTo(self.contentView).offset(12);
        make.left.equalTo(self.iconImageView);
        //        make.left.equalTo(self.iconImageView.mas_left).offset(8);
        make.right.equalTo(self.bgView).offset(-12);
    }];
    
    
    
    
    
    [self.unReadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.titleLabel);
        //        make.left.equalTo(self.titleLabel.mas_left).offset(-2);
        make.left.equalTo(self.iconImageView).offset(0);
        make.size.mas_equalTo(CGSizeMake(5, 5));
    }];
    
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconImageView);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(8);
        make.right.equalTo(self.bgView).offset(-12);
        //        make.bottom.equalTo(self.separatorLine).offset(-8);
        make.bottom.equalTo(self.bgView).offset(-62);
    }];
    
    [self.separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView).offset(12);
        make.right.equalTo(self.bgView).offset(-12);
        //        make.bottom.equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(0.5);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(8);
    }];
    
    [self.sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-12);
        make.top.equalTo(self.separatorLine.mas_bottom).offset(12);
        //        make.bottom.equalTo(self.bgView).offset(-12);
        make.size.mas_equalTo(CGSizeMake(80, 28));
    }];
}

- (void)setModel:(ZJMessageListModel *)model {
    _model = model;
    
    // 设置图标文字
    NSString *firstChar = model.templateNickname.length > 0 ? [model.templateNickname substringToIndex:1] : @"";
    self.iconLabel.text = firstChar;
    
    self.templateLabel.text = model.templateNickname;
    self.titleLabel.text = model.title;
    self.contentLabel.text = model.templateContent;
    self.timeLabel.text = model.createTime;
    self.statusLabel.text = @"";
    if (model.readStatus == 1) {
        self.unReadView.hidden = true;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImageView);
        }];
    } else {
        self.unReadView.hidden = false;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImageView).offset(7);
        }];
    }
    
    if ([_model.templateCode isEqualToString:@"EXAM_INVIGILATE"]  ||
        [_model.templateCode isEqualToString:@"EXAM_RESERVATION_SUCCESS"] ||
        [_model.templateCode isEqualToString:@"EXAM_SUCCESS"] ||
        [_model.templateCode isEqualToString:@"EXAM_FAILED"] ||
        [_model.templateCode isEqualToString:@"BPM_REJECT"] ||
        [_model.templateCode isEqualToString:@"TRANSFER_RESULT"]) {
        [self.sureBtn setTitle:@"查看" forState:UIControlStateNormal];
    } else if ([_model.templateCode isEqualToString:@"BPM_APPROVAL"] ||
               [_model.templateCode isEqualToString:@"EXAM_PUBLISH" ]  ||
               [_model.templateCode isEqualToString:@"TRAIN_START"]||
               [_model.templateCode isEqualToString:@"PASS_SIGN" ]||
               [_model.templateCode isEqualToString:@"TRANSFER_APPROVAL" ]) {
        [self.sureBtn setTitle:@"去处理" forState:UIControlStateNormal];
    } else
//        if ([_model.templateCode isEqualToString:@"PAY_NOTICE"] || [_model.templateCode isEqualToString:@"TRANSFER_CC"] )
        {
        self.sureBtn.hidden = true;
        self.separatorLine.hidden = true;
        [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bgView).offset(-12);
        }];
    }
    
    [self.contentView setNeedsLayout];
    [self.contentView layoutIfNeeded];
}

-(void)clickMessageDetail {
    
    
//    stringUsingEncodingByArr
    
    if (_model.readStatus == 0) {
        ZJMessageReadSatusNetworking * manager = [ZJMessageReadSatusNetworking new];
        manager.Id = self.model.ID;
        [manager dispatchTaskWithParam:@{} completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
            
        }];
        _model.readStatus = 1;
        self.unReadView.hidden = true;
        [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconImageView);
        }];
    }
    
    
    
    if ([_model.templateCode isEqualToString:@"BPM_APPROVAL" ] || [_model.templateCode isEqualToString:@"BPM_REJECT" ]) {
        //审批详情
        PendApprovalInfoViewController * vc = [PendApprovalInfoViewController new];
        //        ZJPendApprovallToDoListModel * model = self.dataSource[indexPath.row];
        vc.taskId =  _model.otherData[@"taskId"];
        vc.processInstanceId = _model.otherData[@"processId"];
        vc.personStr = _model.otherData[@"startUserName"];
        vc.titleStr = _model.otherData[@"processDefinitionName"];
        vc.timeStr = @"casdfvd";
        vc.showOrHidden = [_model.templateCode isEqualToString:@"BPM_APPROVAL" ];
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    } else  if ([_model.templateCode isEqualToString:@"EXAM_INVIGILATE"]) {
        //监考信息
        ZJMessageInvigilateViewController * vc = [ZJMessageInvigilateViewController new];
        //    vc.Id = model.id;
        vc.model = _model;
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
        
    } else  if ([_model.templateCode isEqualToString:@"TRAIN_START"]) {
        //在线培训
        HomeTransactPersonViewController* vc = [HomeTransactPersonViewController new];
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    } else  if ([_model.templateCode isEqualToString:@"EXAM_PUBLISH"] || [_model.templateCode isEqualToString:@"EXAM_RESERVATION_SUCCESS"] || [_model.templateCode isEqualToString:@"EXAM_SUCCESS"] || [_model.templateCode isEqualToString:@"EXAM_FAILED"]) {
        //考试发布消息
        HomeOnlineExaminationViewController* vc = [HomeOnlineExaminationViewController new];
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [vc collectionViewClickIndex:[self->_model.templateCode isEqualToString:@"EXAM_PUBLISH"] ? 0 :[self->_model.templateCode isEqualToString:@"EXAM_RESERVATION_SUCCESS"] ? 1 : 2 ];
            
        });
        
        
    } else  if ([_model.templateCode isEqualToString:@"PASS_SIGN"]) {
        //签收信息
        ZJMessageSignForViewController* vc = [ZJMessageSignForViewController new];
        vc.model = _model;
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    } else  if ([_model.templateCode isEqualToString:@"TRANSFER_RESULT"]  || [_model.templateCode isEqualToString:@"TRANSFER_APPROVAL"]) {
        //转单详情
        HomeTransforShowOrderInfoViewController* vc = [HomeTransforShowOrderInfoViewController new];
        vc.ID = [NSString stringWithFormat:@"%@",_model.otherData[@"transferId"]];
        [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    }
    
//    //    TRANSFER_RESULT  TRANSFER_APPROVAL
    
    //    TRAIN_START
}



#pragma mark - Lazy Loading
- (UIView *)iconImageView {
    if (!_iconImageView) {
        _iconImageView = [[UIView alloc] init];
        _iconImageView.backgroundColor = [UIColor colorWithHexString:@"3271ED"]; // 设置背景色
        _iconImageView.layer.cornerRadius = 10; // 圆形
        _iconImageView.layer.masksToBounds = YES;
    }
    return _iconImageView;
}

- (UILabel *)iconLabel {
    if (!_iconLabel) {
        _iconLabel = [[UILabel alloc] init];
        _iconLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightBold];
        _iconLabel.textColor = [UIColor whiteColor];
        _iconLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _iconLabel;
}

- (UILabel *)templateLabel {
    if (!_templateLabel) {
        _templateLabel = [[UILabel alloc] init];
        _templateLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
        _templateLabel.textColor = [UIColor colorWithHexString:@"181818"];
    }
    return _templateLabel;
}

- (UILabel *)timeLabel {
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.font = [UIFont systemFontOfSize:12];
        _timeLabel.textColor = [UIColor colorWithHexString:@"666666"];
        _timeLabel.textAlignment = NSTextAlignmentRight;
    }
    return _timeLabel;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightBold];
        _titleLabel.textColor = [UIColor colorWithHexString:@"181818"];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _titleLabel;
}
- (UILabel *)statusLabel {
    if (!_statusLabel) {
        _statusLabel = [[UILabel alloc] init];
        _statusLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightBold];
        _statusLabel.textColor = [UIColor colorWithHexString:@"181818"];
        _statusLabel.textAlignment = NSTextAlignmentLeft;
    }
    return _statusLabel;
}



- (UILabel *)contentLabel {
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.font = [UIFont systemFontOfSize:14];
        _contentLabel.textColor = [UIColor colorWithHexString:@"666666"];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

- (UIView *)unReadView {
    if (!_unReadView) {
        _unReadView = [[UIView alloc] init];
        _unReadView.backgroundColor = [UIColor colorWithHexString:@"E34A45"];
        _unReadView.layer.cornerRadius = 2.5; // 圆形
        _unReadView.layer.masksToBounds = YES;
    }
    return _unReadView;
}

- (UIView *)separatorLine {
    if (!_separatorLine) {
        _separatorLine = [[UIView alloc] init];
        _separatorLine.backgroundColor = [UIColor colorWithHexString:@"E4E4E4"];
    }
    return _separatorLine;
}

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 10;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}


- (UIButton *)sureBtn {
    if (!_sureBtn) {
        //        @"查看详情"
        //        _sureBtn = [[UIButton alloc] init];
        //        _sureBtn.titleLabel =[[UILabel alloc] init];
        _sureBtn= [UIButton buttonWithType:UIButtonTypeCustom];
        //        _sureBtn.frame = CGRectMake(0, 0, 80, 28);
        //        [_sureBtn setTitle:@"查看详情" forState:UIControlStateNormal];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sureBtn.titleLabel setFont:[UIFont systemFontOfSize:14 weight:UIFontWeightMedium]];
        _sureBtn.layer.cornerRadius = 4;
        _sureBtn.layer.masksToBounds = true;
        [_sureBtn addTarget:self action:@selector(clickMessageDetail) forControlEvents:UIControlEventTouchUpInside];
        //        [topV addSubview:cancelBtn];
        
        _sureBtn.backgroundColor = [UIColor colorWithHexString:@"3271ED"];
    }
    return _sureBtn;
}


@end
